package com.stxx.stiot2.auth2;

import com.stxx.stiot2.constants.RedisKeys;
import com.stxx.stiot2.external.RedisServer;
import me.zhyd.oauth.cache.AuthStateCache;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;

/**
 * 授权状态缓存
 */
@Component
public class AuthRedisStateCache implements AuthStateCache {

    @Inject
    RedisServer redisServer;

    String getKey(String key){
        return RedisKeys.auth_state_cache + key;
    }

    @Override
    public void cache(String key, String value) {
        // 授权超时时间 默认三分钟
        redisServer.setValueEx(getKey(key),value,180L);
    }

    @Override
    public void cache(String key, String value, long timeout) {
        redisServer.setValueEx(getKey(key),value,timeout/1000);
    }


    @Override
    public String get(String key) {
        return redisServer.getValue(getKey(key),String.class);
    }


    @Override
    public boolean containsKey(String key) {
        return redisServer.exists(getKey(key));
    }
}